package com.xdy.music.stage;

import com.xdy.music.MainApp;
import com.xdy.music.util.XMLUtils;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.List;
import java.util.Optional;

/**
 * 添加歌单舞台
 */
public class AddGroupStage {

    private Stage parentStage;//父窗体
    private VBox vBox;//父窗体中显示歌单列表的VBox对象
    private MainApp mainApp;

    private Stage stage;//添加歌单舞台
    private double mouseX;//鼠标在场景中X轴位置
    private double mouseY;//鼠标在场景中Y轴位置

    private HBox groupHBox;//当前操作歌单HBox

    public AddGroupStage(Stage parentStage, VBox vBox, MainApp mainApp){
        this.parentStage = parentStage;
        this.vBox = vBox;
        this.mainApp = mainApp;
        //开启新的舞台
        stage = new Stage();
        stage.initStyle(StageStyle.UNDECORATED);//去除标题栏
        stage.getIcons().add(new Image("image/upload/icon.jpeg"));
        //创建新的场景
        Group group = new Group();//跟pane类似的容器
        Scene scene = new Scene(group,450,300);
        scene.setFill(Color.rgb(35, 35, 35));
        //当鼠标按下时
        scene.setOnMousePressed(event -> {
            mouseX = event.getSceneX();
            mouseY = event.getSceneY();
        });
        //当鼠标拖拽时
        scene.setOnMouseDragged(event -> {
            stage.setX(event.getScreenX() - mouseX);
            stage.setY(event.getScreenY() - mouseY);
        });
        //新建歌单标题
        Label label = new Label("新建歌单");
        label.setTextFill(Color.WHITE);
        label.setPrefWidth(150);
        label.setPrefHeight(50);
        label.setLayoutX(20);//设置场景内横坐标20
        label.setLayoutY(0);//设置场景内纵坐标0
        //关闭按钮
        ImageView iv1 = new ImageView("image/icon/exit.png");
        iv1.setFitWidth(20);
        iv1.setFitHeight(20);
        Label label1 = new Label("",iv1);
        label1.setMinHeight(0);
        label1.setMinWidth(0);
        label1.setPrefHeight(20);
        label1.setPrefWidth(20);
        label1.setLayoutX(400);
        label1.setLayoutY(20);
        label1.setOnMouseEntered(event -> iv1.setImage(new Image("image/icon/exit-dark.png")));
        label1.setOnMouseExited(event -> iv1.setImage(new Image("image/icon/exit.png")));
        label1.setOnMouseClicked(event -> stage.hide());
        //添加歌单输入框
        TextField addInput = new TextField();
        addInput.setPromptText("请输入歌单名称");
        addInput.setPrefWidth(240);
        addInput.setPrefHeight(35);
        addInput.setLayoutX(110);
        addInput.setLayoutY(120);
        //提示信息标签
        Label tipLabel = new Label();
        tipLabel.setPrefWidth(240);
        tipLabel.setLayoutX(110);
        tipLabel.setLayoutY(165);
        tipLabel.setTextFill(Color.DARKRED);
        //确定按钮
        Button btnOk = new Button("确定");
        btnOk.setPrefHeight(30);
        btnOk.setPrefWidth(80);
        btnOk.setLayoutX(110);
        btnOk.setLayoutY(210);
        btnOk.setTextFill(Color.rgb(200,200,200));
        btnOk.setBackground(new Background(new BackgroundFill(Color.rgb(50,45,128),null,null)));
        btnOk.setOnMouseEntered(event -> {
            btnOk.setTextFill(Color.rgb(255,255,255));
            btnOk.setCursor(Cursor.HAND);//设置鼠标成小手
        });
        btnOk.setOnMouseExited(event -> btnOk.setTextFill(Color.rgb(200,200,200)));
        btnOk.setOnMouseClicked(event -> {
            //获取文本框中的歌单名称
            String groupName = addInput.getText();
            //判断输入歌单是否为空
            if (groupName == null || groupName.trim().length() == 0){
                tipLabel.setText("请输入有效歌单名称！");
                return;
            }
            //判断歌单是否已经存在
            List<String> groupList = XMLUtils.readAllGroup();
            for (String s : groupList) {
                if (groupName.equals(s)){
                    tipLabel.setText("歌单[" + groupName +"]已经存在！");
                    return;
                }
            }
            //歌单不存在，添加歌单
            XMLUtils.addGroup(groupName);

            //更新主窗体中的歌单列表
            //添加到喜欢图标
            ImageView iv3 = new ImageView("image/icon/favorite.png");
            iv3.setFitWidth(30);
            iv3.setPreserveRatio(true);
            Label label5 = new Label("",iv3);
            label5.setMinWidth(0);
            label5.setMinHeight(0);
            label5.setPrefWidth(30);
            label5.setPrefHeight(30);
            label5.setOnMouseEntered(ee -> iv3.setImage(new Image("image/icon/favorite-dark.png")));
            label5.setOnMouseExited(ee -> iv3.setImage(new Image("image/icon/favorite.png")));
            //歌单名称
            Label label6 = new Label(groupName);
            label6.setMinWidth(0);
            label6.setPrefHeight(30);
            label6.setPrefWidth(150);
            label6.setTextFill(Color.rgb(210,210,210));
            label6.setOnMouseEntered(ee -> label6.setTextFill(Color.rgb(250,250,250)));
            label6.setOnMouseExited(ee -> label6.setTextFill(Color.rgb(210,210,210)));
            //播放图片
            ImageView iv4 = new ImageView("image/icon/laba.png");
            iv4.setFitWidth(30);
            iv4.setPreserveRatio(true);
            Label label7 = new Label("",iv4);
            label7.setMinWidth(0);
            label7.setMinHeight(0);
            label7.setPrefWidth(30);
            label7.setPrefHeight(30);
            label7.setOnMouseEntered(ee -> iv4.setImage(new Image("image/icon/laba-dark.png")));
            label7.setOnMouseExited(ee -> iv4.setImage(new Image("image/icon/laba.png")));
            //添加图标
            ImageView iv5 = new ImageView("image/icon/add.png");
            iv5.setFitWidth(30);
            iv5.setPreserveRatio(true);
            Label label8 = new Label("",iv5);
            label8.setMinWidth(0);
            label8.setMinHeight(0);
            label8.setPrefWidth(30);
            label8.setPrefHeight(30);
            label8.setOnMouseEntered(ee -> iv5.setImage(new Image("image/icon/add-dark.png")));
            label8.setOnMouseExited(ee -> iv5.setImage(new Image("image/icon/add.png")));
            //删除图标
            ImageView iv6 = new ImageView("image/icon/delete.png");
            iv6.setFitWidth(30);
            iv6.setPreserveRatio(true);
            Label label9 = new Label("",iv6);
            label9.setMinWidth(0);
            label9.setMinHeight(0);
            label9.setPrefWidth(30);
            label9.setPrefHeight(30);
            label9.setOnMouseEntered(ee -> iv6.setImage(new Image("image/icon/delete-dark.png")));
            label9.setOnMouseExited(ee -> iv6.setImage(new Image("image/icon/delete.png")));
            label9.setOnMouseClicked(ee -> {
                //删除歌单
                //询问用户确认删除吗
                Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
                alert.setTitle("删除歌单");
                alert.setContentText("确认删除[" + groupName + "]吗？");
                Optional<ButtonType> buttonType = alert.showAndWait();
                if (buttonType.get().equals(ButtonType.OK)){
                    //点击确认，删除歌单
                    XMLUtils.deleteGroup(groupName);
                    //更新歌单VBox
                    vBox.getChildren().remove(groupHBox);
                }
            });
            //把他们封装到一个HBox中
            groupHBox = new HBox(10);
            groupHBox.setPrefWidth(300);
            groupHBox.getChildren().addAll(label5,label6,label7,label8,label9);
            groupHBox.setPadding(new Insets(5,5,5,20));
            this.vBox.getChildren().add(groupHBox);
            //关闭添加歌单舞台
            stage.hide();
        });
        //取消按钮
        Button btnCancel = new Button("取消");
        btnCancel.setPrefHeight(30);
        btnCancel.setPrefWidth(80);
        btnCancel.setLayoutX(270);
        btnCancel.setLayoutY(210);
        btnCancel.setTextFill(Color.rgb(200,200,200));
        btnCancel.setBackground(new Background(new BackgroundFill(Color.GRAY,null,null)));
        btnCancel.setOnMouseEntered(event -> {
            btnCancel.setTextFill(Color.rgb(255,255,255));
            btnCancel.setCursor(Cursor.HAND);
        });
        btnCancel.setOnMouseExited(event -> btnCancel.setTextFill(Color.rgb(200,200,200)));
        btnCancel.setOnMouseClicked(event -> stage.hide());
        group.getChildren().addAll(label,label1,addInput,tipLabel,btnOk,btnCancel);
        //设置场景
        stage.setScene(scene);
        //显示舞台
        stage.show();
    }

}
